package org.lmind.example.uc.webapp;

import javax.annotation.Resource;

import org.lmind.example.common.api.auth.AuthenticationProvider;
import org.lmind.example.common.api.auth.Certificate;
import org.lmind.example.common.api.auth.Credential;
import org.lmind.example.common.api.auth.NamePasswordCredential;
import org.lmind.example.common.core.jpa.EntityManagerHelper;
import org.lmind.example.uc.api.UserCenter;
import org.lmind.example.uc.api.UserInfo;

public class WebAppAuthenticationProvider implements AuthenticationProvider {
	
	@Resource
	private EntityManagerHelper emh;
	
	@Resource
	private UserCenter userCenter;

	@Override
	public boolean supports(Class<?> credential) {
		return NamePasswordCredential.class.isAssignableFrom(credential);
	}

	@Override
	public Certificate authenticate(Credential certificate) {
		// TODO Auto-generated method stub
		NamePasswordCredential cert = (NamePasswordCredential)certificate;
		UserInfo userInfo = userCenter.login(cert.getName(), cert.getPassword());
		return userInfo;
	}

}
